package com.gec.controller;

import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.gec.pojo.Article;
import com.gec.pojo.Reply;
import com.gec.pojo.User;
import com.gec.service.ArticleService;
import com.gec.service.ReplyService;
import com.gec.service.UserService;


@Controller("articleController")
public class ArticleController {
	
	@Autowired
	ArticleService articleService;
	@Autowired
	ReplyService rs;
	@Autowired
	UserService us;
	
	@RequestMapping("/index.do")
	public String index(String typecode,String keyword,String currentPage,HttpServletRequest req,HttpServletResponse resp) throws UnsupportedEncodingException {
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		List<Article> aList = articleService.getArticles();
		req.getSession().setAttribute("aList", aList);
		return "index";
	}
	
	@RequestMapping("/index2")
	public String index2(String typecode,String keyword,String currentPage,HttpServletRequest req,HttpServletResponse resp) throws UnsupportedEncodingException {
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		List<Article> aList = articleService.getArticles();
		req.getSession().setAttribute("aList", aList);
		return "index2";
	}
	
	@RequestMapping("/life")
	public String getArticleAtLife(HttpServletRequest req,HttpServletResponse resp) {
		List<Article> aList = articleService.getArticleByType("0001");
		req.getSession().setAttribute("aList", aList);
		return "index2";
	}
	
	@RequestMapping("/entertainment")
	public String getArticleAtEm(HttpServletRequest req,HttpServletResponse resp) {
		List<Article> aList = articleService.getArticleByType("0002");
		req.getSession().setAttribute("aList", aList);
		return "index2";
	}
	
	@RequestMapping("/school")
	public String getArticleAtSchool(HttpServletRequest req,HttpServletResponse resp) {
		List<Article> aList = articleService.getArticleByType("0003");
		req.getSession().setAttribute("aList", aList);
		return "index2";
	}
	
	@RequestMapping("/wantPost")
	public String wantPost(HttpServletRequest req,HttpServletResponse resp) {
		return "article";
	}
	
	@RequestMapping("/postArticle")
	public String postArticle(String title,String text,String life,String em,String school,HttpServletRequest req,HttpServletResponse resp) {
		User user = (User) req.getSession().getAttribute("session_user");
		String name = user.getName();
		String typecode = "";
		if(life != null) {
			typecode = "0001";
		}else if(em != null) {
			typecode = "0002";
		}else if(school != null){
			typecode = "0003";
		}
		articleService.postArticle(title,text,name,typecode);
		us.expUP((user.getExp()+100),name);
		Article art = articleService.getNewestArticle();
		return "redirect:data?id=" + art.getAid();
	}
	
	@RequestMapping("/look")
	public String look(int id,HttpServletRequest req,HttpServletResponse resp) {
		User user = (User) req.getSession().getAttribute("session_user");
		us.LVcheck(user.getExp(),user.getLv(),user.getName());
		Article a = articleService.getArticleById(id);
		User owner = us.getUserByName(a.getName());
		List<Reply> rList = rs.getReplyByAid(id);
		req.getSession().setAttribute("article", a);
		req.getSession().setAttribute("owner", owner);
		req.getSession().setAttribute("reply", rList);
		return "details";
	}
	
	@RequestMapping("/delete")
	public String delete(HttpServletRequest req,HttpServletResponse resp) {
		Article art = (Article) req.getSession().getAttribute("article");
		articleService.deleteArticle(art.getAid());
		return "redirect:index2";
	}
	
	@RequestMapping("/edit")
	public String edit(HttpServletRequest req,HttpServletResponse resp) {
		return "edit";
	}
	
	@RequestMapping("/editArt")
	public String editArt(String title,String text,HttpServletRequest req,HttpServletResponse resp) {
		Article art = (Article) req.getSession().getAttribute("article");
		articleService.updateArticle(art.getAid(),title,text);
		return "redirect:look?id="+art.getAid()+"&name="+art.getName();
	}
}
